//
//  CommentView.m
//  Final
//
//  Created by student on 2021/12/14.
//  Copyright © 2021 Lenrony. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "CommentView.h"

@interface CommentView()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>{
    
    float screen_width;
    float screen_height;
    float comments_height;
}

@property(strong, nonatomic) NSMutableArray* rawusers;
@property(strong, nonatomic) NSMutableArray* contents;
@property(strong, nonatomic) NSMutableArray* timestamps;

@property(strong, nonatomic) UIViewController* svc;

@end

@implementation CommentView

-(CommentView*)initWithRawUsers: (NSMutableArray*)rawusers
                    andContents: (NSMutableArray*)contents
                       andTimes: (NSMutableArray*)timestamps
              andViewController: (UIViewController*)svc
{
    self = [super init];
    self.rawusers = rawusers;
    self.contents = contents;
    self.timestamps = timestamps;
    self.svc = svc;
    screen_width = [UIScreen mainScreen].bounds.size.width;
    screen_height = [UIScreen mainScreen].bounds.size.height;
    comments_height = 200;
    
    //每一个格子布局
    UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(screen_width, 200);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = UIEdgeInsetsMake(4, 0, 0, 0);
    
    //View布局
    self.commentLines = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, screen_width, 100 * contents.count) collectionViewLayout:layout];
    self.commentLines.backgroundColor = [UIColor clearColor];
    self.commentLines.scrollEnabled = YES;
    self.commentLines.userInteractionEnabled = YES;//起初允许交互
    
    //设置代理、注册
    self.commentLines.delegate = self;
    self.commentLines.dataSource = self;
    [self.commentLines registerClass:[CommentCell class] forCellWithReuseIdentifier:@"Comments"];
    [self addSubview: self.commentLines];
    
    return self;
}

//告诉代理返回的cell是什么样子的
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CommentCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Comments" forIndexPath:indexPath];
    int ind = (int)indexPath.row;
    [cell setRawUser:self.rawusers[ind]
          andContent:self.contents[ind]
             andTime:self.timestamps[ind]
   andViewController:self.svc];
    
    return cell;
}

//一共几个cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.contents.count;
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    int ind = (int)indexPath.row;
    CGSize size = [self.contents[ind] boundingRectWithSize:CGSizeMake(screen_width - 10, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} context:nil].size;
    size = CGSizeMake(screen_width - 10, size.height + 55);
    comments_height += size.height;
    self.commentLines.frame = CGRectMake(0, 0, screen_width, comments_height);
    return size;
}
@end
